/**
 * 模块化开发
 */ 

window.onload = function (){
  init();
}

function init() {
  SearchKw();
}

var SearchKw = (function() {
  var oAutoKw = document.getElementById('J_autoKw'),
      oSearch = document.getElementById('J_search_kw'),
      recomKw = JSON.parse(document.getElementById('J_recomkw').innerHTML)
      recomKwIndex = 0
      t = null;


  addEvent(oSearch, 'focus', function() {
    clearInterval(t);
    oAutoKw.style.color = '#ccc';
  });

  addEvent(oSearch, 'blur', function() {
    autoKwShow(this.value, true);
    t = setInterval(autoKw, 1000);
  });

  addEvent(oSearch, 'input', function() {
    autoKwShow(this.value, false);
  });

  addEvent(oSearch, 'propertychange', function() {
    autoKwShow(this.value, false);
  });

  // 判断关键词是否轮播跟颜色状态的切换
  function autoKwShow(val, isBlur) {
    if(val.length > 0) {
      oAutoKw.className = 'auto-kw hide';
    }else if(isBlur) { // 失去焦点
      oAutoKw.className = 'auto-kw show';
      oAutoKw.style.color = '#989898';
    }else {
      oAutoKw.className = 'auto-kw show';
    }
    
  }

  function autoKw () {
    var key = recomKw[recomKwIndex],
        len = recomKw.length;
    oAutoKw.innerHTML = key;
    recomKwIndex = recomKwIndex >= (len - 1) 
                                ? 0 
                                : recomKwIndex + 1;
  }

  function setAutoKw() {
    autoKw();
    t = setInterval(autoKw, 1000);
  }

  return function() {
    setAutoKw();
  };

})();